package com.xyz.dao.test;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GenLogic {
    private String LOGIC_FORMAT =
            "package com.xyz.dao.logic;\n" +
            "\n" +
            "import com.free.ext.mybatis.model.entity.PageResponseModel;\n" +
            "import com.xyz.dao.dao.{DaoNm};\n" +
            "import com.xyz.domain.table.{ClazzNm};\n" +
            "import org.springframework.beans.factory.annotation.Autowired;\n" +
            "import org.springframework.stereotype.Component;\n" +
            "\n" +
            "import java.util.List;\n" +
            "\n" +
            "@Component\n" +
            "public class {LogicNm} {\n" +
            "\n" +
            "    @Autowired\n" +
            "    private {DaoNm} {DaoTg};\n" +
            "\n" +
            "    public {ClazzNm} add({ClazzNm} {EntNm}){\n" +
            "        return {DaoTg}.insert({EntNm});\n" +
            "    }\n" +
            "\n" +
            "    public Integer update({ClazzNm} {EntNm}){\n" +
            "        return {DaoTg}.update({EntNm});\n" +
            "    }\n" +
            "\n" +
            "    public List<{ClazzNm}> list({ClazzNm} {EntNm}, String orderBy){\n" +
            "        return {DaoTg}.selectList({EntNm}, orderBy);\n" +
            "    }\n" +
            "\n" +
            "    public Integer delete(Integer key){\n" +
            "        return {DaoTg}.delete(key);\n" +
            "    }\n" +
            "\n" +
            "    public PageResponseModel<{ClazzNm}> pageInfo({ClazzNm} {EntNm}, String orderBy, Integer pageNo, Integer pageSize){\n" +
            "        return {DaoTg}.selectByPageNo({EntNm}, orderBy, pageNo, pageSize);\n" +
            "    }\n" +
            "\n" +
            "}";

    @Test
    public void genLogic(){
        List<String> list = new ArrayList<>();
        list.add("CCustomerOperLog");
        //list.add("SCustInfoTemp");
        //        list.add("CAnnounceInfo");
//        list.add("SLotteryStatisDay");
//        list.add("SLotteryStatisMonth");
        //        list.add("SLotteryPeriodRes");
//        list.add("SLotteryPeriod");
//        list.add("CCustomerfriend");
//        list.add("CCustomerfund");
//        list.add("CCustomerfundflow");
//        list.add("CCustomerrechargelog");
//        list.add("CCustomersign");
//        list.add("CCustomerviplog");
//        list.add("CGiftlog");
//        list.add("CRecommendhistory");
//        list.add("OCustomergameaccnt");
//        list.add("OCustomergamelog");
//        list.add("OGame");
//        list.add("OGamerule");
//        list.add("OGamespecrule");
//        list.add("OGamestatisrule");
//        list.add("OGamewordrule");
//        list.add("SAnnouncement");
//        list.add("SGift");
//        list.add("SLotteryBetinfo");
//        list.add("SLotteryCustruledetail");
//        list.add("SLotteryCustrule");
//        list.add("SLotterydetail");
//        list.add("SLottery");
//        list.add("SSetting");
//        list.add("SVipDetail");
//        list.add("SVip");
        String path = "D://workspace//selfprjs//extrawork//javaprjs//xingyunzhuan//parent//dao//src//main//java//com//xyz//dao//logic//%s";
        list.forEach(x->{
            String daoFileNm = x + "Logic.java";
            String daoNm = x + "Dao";
            String logicNm = x + "Logic";
            String daoTg = tmpNm(x) + "Dao";
            String content = LOGIC_FORMAT.replaceAll("\\{ClazzNm\\}", x);
            content = content.replaceAll("\\{EntNm\\}",tmpNm(x));
            content = content.replaceAll("\\{DaoNm\\}",daoNm);
            content = content.replaceAll("\\{LogicNm\\}",logicNm);
            content = content.replaceAll("\\{DaoTg\\}",daoTg);
            String filePath = String.format(path,daoFileNm);
            File f = new File(filePath);
            try {
                if(!f.exists()){
                    f.createNewFile();
                }
                FileWriter fileWritter = new FileWriter(f,true);
                fileWritter.write(content);
                fileWritter.flush();
                fileWritter.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    private String tmpNm(String keyNm){
       String firstWord =  keyNm.substring(0,1).toLowerCase();
       StringBuffer sb = new  StringBuffer();
       sb.append(firstWord).append(keyNm.substring(1));
       return sb.toString();
    }
}
